// pages/order/succeed/succeed.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //取餐号
    num:"123456",
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var myDate = new Date();
    var that = this;
    const db = wx.cloud.database()
    var Num="";
    for(var i=0;i<6;i++)
    {
      Num+=Math.floor(Math.random()*10);
    }
    // console.log(Num);
    this.setData({
      num:Num
    })

    // console.log(options);
    var id = options.id;
    // console.log(options.id);
    var meal_price = options.price;
    db.collection('meal').where({
      _id: options.id
      
    })
    .get({
      success: function(res) {
        // res.data 是包含以上定义的两条记录的数组
        // console.log(res.data[0])
        var meal_img = res.data[0].meal_img;
        var meal_name = res.data[0].meal_name;
        // var meal_price = res.data[0].meal_price;
        var window_id = res.data[0].window_id;
        var canteen_id = res.data[0].canteen_id;
        wx.getStorage({
          key: 'myData',
            success: function(res) {
            // console.log(res.data);
            var student_id = res.data.student_id;
            var student_name = res.data.student_name;
            var student_phone = res.data.student_phone;
            db.collection('order_info').add({
              // data 字段表示需新增的 JSON 数据
              data: {
                canteen_id:canteen_id,
                meal_img:meal_img,
                meal_name:meal_name,
                meal_price:meal_price,
                order_random:that.data.num,
                order_status:"0",
                order_time:myDate.toLocaleString(),
                student_id:student_id,
                student_name:student_name,
                student_phone:student_phone,
                window_id:window_id
              },
              success: function(res) {
                // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
                // console.log(res)
              }
            })
          }
         
         })
      }
    })
    
     
     
   

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  order(){
    wx.navigateTo({
      url: '/pages/order/list/list',
    })
  },
  shouye(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  }
})